'''
A number of default scripts are defined here. These may be added to by creating
classes inherited from Menu. They have the following attributes:

   class name  The name of the class is used as the command name in the menu and
               in the undo list. Use an underscore to represent a space.

   docstring   The class docstring is used as context sensitive help for the
               command.

   run(self)   The run function is called when the menu item is selected. This
               is the command action.

An example is:
class Select_All(Menu):
   """This command selects everything."""
   path = 'Edit|Select'
   def run(self): doc().select_all()

The commands are read from this file is order, so they will appear in the menus
in the order in which they are typed here. To create a separator, define a
class inherited from Separator. This class has only a path attribute.

'''

from docmanager import DocManager
from actionbase import Action
from stdwidgets import *
from conversions import *
import wizards
import gui


GUI = lambda: gui.Manager()
# ---------------------------------------------------------------------------
#  FILE commands
# ---------------------------------------------------------------------------

class NewDoc(Action):
   '''Create a new document.'''
   suppress_command = None
   text = '&New'
   call = lambda self: DocManager().new()

class OpenDoc(Action):
   '''Open a document.'''
   suppress_command = None
   text = '&Open'
   call = lambda self: DocManager().open()

class SaveDoc(Action):
   '''Save the document'''
   suppress_command = None
   text = '&Save'
   requires = DocManager().evt_opened
   call = lambda self: DocManager().save()

class SaveDocAs(Action):
   '''Save the document under a new name'''
   suppress_command = None
   text = 'Save &As'
   requires = DocManager().evt_opened
   call = lambda self: DocManager().save()

class CloseDoc(Action):
   '''Close the open document.'''
   suppress_command = None
   text = '&Close'
   requires = DocManager().evt_opened
   call = lambda self: DocManager().close()

class Exit(Action):
   '''Exit the program.'''
   suppress_command = None
   text = '&Exit'
   def call(self):
      if DocManager().close():
         qApp.quit()

# ---------------------------------------------------------------------------
#  HELP commands
# ---------------------------------------------------------------------------

class AboutDlg(Action):
   '''Display an about box.'''
   supress_command = None
   text = '&About'
   call = lambda self: Msg().About()

class AboutQtDlg(Action):
   '''Display About Qt.'''
   supress_command = None
   text = '&About Qt'
   call = lambda self:qApp.aboutQt()

# ---------------------------------------------------------------------------
#  VIEW commands
# ---------------------------------------------------------------------------

class OverlayMaster(Action):
   '''Show all items in the master, whether they are included in the schedule or not.'''
   text = '&Overlay Master'
   checked = True
   toggle = gui.Manager().evt_overlay_master
   
# ---------------------------------------------------------------------------
#  CURRENT commands
#
#  These act on the currently active table and are meant to be used 
#  in context menus.
# ---------------------------------------------------------------------------

class InsertRowCurrent(Action):
   '''Add a row'''
   text = 'Insert row'
   #requires = gui.Manager().evt_current
   call = lambda self: GUI().currentModel().insertRow()

class DeleteRowCurrent(Action):
   '''Delete the selected row'''
   text = 'Delete row'
   #requires = gui.Manager().schedules.evt_select
   call = lambda self: GUI().currentModel().deleteRow()

class InsertColumnCurrent(Action):
   '''Delete the selected column'''
   text = 'Insert column'
   #requires = gui.Manager().schedules.evt_select
   call = lambda self: GUI().currentView().insertColumn()

class InsertSpecialColumn(Action):
   '''Add a special column, e.g. an amount or total'''
   text = 'Add special column'
   requires = GUI().evt_currentview
   def call(self):
      # Get view first, because it will be reset when getInterface is called
      view = GUI().currentView()
      interface = getColumnInterface()
      if interface is not None:
         view.insertColumnWithArgs(interface=interface())

class DeleteColumnCurrent(Action):
   '''Delete the selected column'''
   text = 'Delete column'
   #requires = gui.Manager().schedules.evt_select
   call = lambda self: GUI().currentView().deleteColumn()


# ---------------------------------------------------------------------------
#  SCHEDULE commands
# ---------------------------------------------------------------------------

class InsertSchedule(Action):
   '''Add a schedule'''
   text = '&Insert schedule'
   requires = DocManager().evt_opened
   call = lambda self: GUI().scheduleView().insertRow(GUI().currentSchedule())

class DeleteSchedule(Action):
   '''Delete the current schedule.'''
   text = '&Delete current schedule'
   requires = GUI().schedules.evt_select
   call = lambda self: GUI().schedules.deleteRow(GUI().currentSchedule())

#class MoveScheduleUp(Action):
#   '''Move the schedule up a row.'''
#   text = 'Move schedule &up'
#   requires = gui.Manager().schedules.evt_select
#   call = lambda self: move(DocManager().doc.scheduletable.rows, Current().schedule('idx'), Current().schedule('idx') - 1)
#
#class MoveScheduleDown(Action):
#   '''Move the schedule down a row.'''
#   text = 'Move schedule &down'
#   requires = gui.Manager().schedules.evt_select
#   call = lambda self: move(DocManager().doc.scheduletable.rows, Current().schedule('idx'), Current().schedule('idx') + 1)
#

# ---------------------------------------------------------------------------
#  MASTER commands
# ---------------------------------------------------------------------------

#Todo: Instead of this, create a property for the row: 'active in current schedule'
#class AddToSchedule(Action):
#   '''Add the selected section to the current schedule.'''
#   text = 'Add to schedule'
#   requires = gui.Manager().schedules.evt_select #& (Current().master_section.evt | Current().master_item.evt)
#   call = lambda self: Current().schedule.sectiontable.rows.append(Current().section())

# ---------------------------------------------------------------------------
#  SECTION commands
# ---------------------------------------------------------------------------

class AppendSection(Action):
   '''Add a section'''
   text = '&Append section'
   #requires = DocManager().evt_opened
   call = lambda self: DocManager().doc.master.append(level='section')

class InsertSection(Action):
   '''Add a section'''
   text = '&Insert section'
   #requires = Current().master_section.evt
   call = lambda self: DocManager().doc.master.insertnew(Current().master_section('idx'), level=section)

class DeleteSection(Action):
   '''Delete the current section.'''
   text = '&Delete current section'
   #requires = Current().master_section.evt
   def call(self):
      del DocManager().doc.master[Current().master_section('idx')]

class MoveSectionUp(Action):
   '''Move the section up a row.'''
   text = 'Move section &up'
   #requires = Current().master_section.evt
   call = lambda self: move(DocManager().doc.master, Current().master_section('idx'), Current().master_section('idx') - 1)

class MoveSectionDown(Action):
   '''Move the section down a row.'''
   text = 'Move section &down'
   #requires = Current().master_section.evt
   call = lambda self: move(DocManager().doc.master, Current().master_section('idx'), Current().master_section('idx') + 1)


# ---------------------------------------------------------------------------
#  ITEM commands
# ---------------------------------------------------------------------------

class AppendItem(Action):
   '''Add a item'''
   text = '&Append item'
   #requires = Current().master_section.evt
   call = lambda self: Current().master_section().append()

class InsertItem(Action):
   '''Add a item'''
   text = '&Insert item'
   #requires = Current().master_item.evt
   call = lambda self: Current().master_section().insertnew(Current().master_item('idx'))

class DeleteItem(Action):
   '''Delete the current item.'''
   text = '&Delete current item'
   #requires = Current().master_item.evt
   def call(self):
      del Current().master_section[Current().master_item('idx')]

class MoveItemUp(Action):
   '''Move the item up a row.'''
   text = 'Move item &up'
   #requires = Current().master_item.evt
   call = lambda self: move(Current().master_section(), Current().master_item('idx'), Current().master_item('idx') - 1)

class MoveItemDown(Action):
   '''Move the item down a row.'''
   text = 'Move item &down'
   #requires = Current().master_item.evt
   call = lambda self: move(Current().master_section(), Current().master_item('idx'), Current().master_item('idx') + 1)

